package com.petmagics.learnanimation;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.Toast;

/**
 * 混合动画&动画监听
 */
public class AnimationBlendingFm extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_animation_blending, container,false);

        root.findViewById(R.id.btnBack).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getFragmentManager().popBackStack();
            }
        });

        root.findViewById(R.id.btnAnimMe).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //code mode
                /*AnimationSet as = new AnimationSet(true); //是否共用动画补间 (例如：加速执行，减速执行等)
                as.setDuration(1000);
                AlphaAnimation aa = new AlphaAnimation(0,1);
                aa.setDuration(1000);
                as.addAnimation(aa);
                TranslateAnimation ta = new TranslateAnimation(200,0,200,0);
                ta.setDuration(1000);
                as.addAnimation(ta);*/

                //xml mode
                Animation a = AnimationUtils.loadAnimation(getActivity(), R.anim.anim);
                //动画效果监听
                a.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        Toast.makeText(getActivity(), "Animation end", Toast.LENGTH_SHORT).show();
                    }

                    @Override //重复时
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                view.startAnimation(a);
            }
        });

        return root;
    }
}
